package com.telit.firecommand.controller;

import com.telit.common.entity.DataReturn;
import com.telit.common.entity.PageData;
import com.telit.common.util.JwtTokenUtils;
import com.telit.common.util.MinioUtils;
import com.telit.common.util.TimeUtils;
import com.telit.firecommand.entity.CameraDownloadLog;
import com.telit.firecommand.service.CameraDownloadLogService;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;

import javax.servlet.http.HttpServletResponse;


/**
 * @author zhouyu
 * @description 历史视频下载查询接口
 */
@RestController
@RequestMapping("/camera-download-log")
@Api(tags = "历史视频下载查询接口", description = "/camera-download-log")
@Slf4j
public class CameraDownloadLogController {

    @Autowired
    private CameraDownloadLogService cameraDownloadLogService;


    /* *//**
     * 2022-07-06无数据权限
     *
     * @param page
     * @param rows
     * @param cameraName
     * @param startTime
     * @param endTime
     * @return
     *//*
   @GetMapping("/queryByCondition")
    @ApiOperation("条件查询")
    public DataReturn queryByCondition(@RequestParam(required = true) Integer page, @RequestParam(required = true) Integer rows, @RequestParam(required = false) String cameraName, @RequestParam(required = false) String startTime, @RequestParam(required = false) String endTime) {
        PageData<Object> pageData = cameraDownloadLogService.queryByCondition(page, rows, cameraName, startTime, endTime);
        return DataReturn.ok(pageData);
    }*/

    /**
     * 2022-07-06 有数据权限
     *
     * @param page
     * @param rows
     * @param cameraName
     * @param startTime
     * @param endTime
     *
     * @return
     */
    @GetMapping("/queryByCondition")
    @ApiOperation("条件查询")
    public DataReturn queryByCondition(@RequestParam(required = true) Integer page,
                                       @RequestParam(required = true) Integer rows,
                                       @RequestParam(required = false) String cameraName,
                                       @RequestParam(required = false) String startTime,
                                       @RequestParam(required = false) String endTime) {
        PageData<Object> pageData = cameraDownloadLogService.queryByConditionWithDistrictCode(
                JwtTokenUtils.getDistrictCode(), page, rows, cameraName, startTime, endTime);
        return DataReturn.ok(pageData);
    }


    @GetMapping(value = "/downloadToClient")
    @ApiOperation("视频下载到客户端")
    public DataReturn downloadToClient(@RequestParam(required = true) Long id, HttpServletResponse response) {
        CameraDownloadLog cameraDownloadLog = cameraDownloadLogService.queryById(id);
        String videoUrl = cameraDownloadLog.getVideoUrl();
        String filename = cameraDownloadLog.getCameraName() + "-" + TimeUtils.convertFileNameTime(
                cameraDownloadLog.getStartTime()) + "-" + TimeUtils.convertFileNameTime(
                cameraDownloadLog.getStartTime()) + ".mp4";
        // 设置header头,避免网关记录日志打印response进入kafka
        response.setHeader("Content-Type", "video/mp4");
        MinioUtils.download(videoUrl, filename, response);
        return DataReturn.ok();
    }

}
